<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>处理结果</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <style>
        .table-container {
            max-height: 500px;
            overflow: auto;
            margin-bottom: 1rem;
        }
        .table-container table {
            margin-bottom: 0;
        }
        .card {
            margin-bottom: 1rem;
        }
        .chart-container {
            height: 500px;
        }
    </style>
</head>
<body>
    <div class="container-fluid mt-4">
        <h2 class="text-center mb-4">Excel处理结果</h2>
        
        <!-- 图表展示 -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h4 class="mb-0">年末利息累积和最大前五个项目的变化趋势</h4>
                    </div>
                    <div class="card-body">
                        <div id="chart" class="chart-container"></div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 表格展示 -->
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <div class="d-flex justify-content-between align-items-center">
                            <ul class="nav nav-tabs card-header-tabs" id="myTab" role="tablist">
                                <li class="nav-item" role="presentation">
                                    <button class="nav-link active" id="daily-diff-tab" data-bs-toggle="tab" 
                                            data-bs-target="#daily-diff" type="button" role="tab">每日差额表</button>
                                </li>
                                <li class="nav-item" role="presentation">
                                    <button class="nav-link" id="cum-diff-tab" data-bs-toggle="tab" 
                                            data-bs-target="#cum-diff" type="button" role="tab">差额累积和表</button>
                                </li>
                                <li class="nav-item" role="presentation">
                                    <button class="nav-link" id="daily-interest-tab" data-bs-toggle="tab" 
                                            data-bs-target="#daily-interest" type="button" role="tab">每日利息表</button>
                                </li>
                                <li class="nav-item" role="presentation">
                                    <button class="nav-link" id="cum-interest-tab" data-bs-toggle="tab" 
                                            data-bs-target="#cum-interest" type="button" role="tab">利息累积和表</button>
                                </li>
                            </ul>
                            <a href="{{ url_for('download', filename=output_file) }}" class="btn btn-primary">
                                下载Excel文件
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="tab-content" id="myTabContent">
                            <div class="tab-pane fade show active" id="daily-diff" role="tabpanel">
                                <div class="table-container">
                                    {{ dfc_table | safe }}
                                </div>
                            </div>
                            <div class="tab-pane fade" id="cum-diff" role="tabpanel">
                                <div class="table-container">
                                    {{ dfd_table | safe }}
                                </div>
                            </div>
                            <div class="tab-pane fade" id="daily-interest" role="tabpanel">
                                <div class="table-container">
                                    {{ dfe_table | safe }}
                                </div>
                            </div>
                            <div class="tab-pane fade" id="cum-interest" role="tabpanel">
                                <div class="table-container">
                                    {{ dff_table | safe }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 解析图表数据
        const dates = JSON.parse('{{ dates | tojson | safe }}');
        const projectData = JSON.parse('{{ project_data | tojson | safe }}');
        
        // 准备图表数据
        const traces = [];
        for (const [project, values] of Object.entries(projectData)) {
            traces.push({
                x: dates,
                y: values,
                name: project,
                type: 'scatter',
                mode: 'lines'
            });
        }

        // 绘制图表
        Plotly.newPlot('chart', traces, {
            title: '年末利息累积和最大前五个项目的变化趋势',
            xaxis: { title: '日期' },
            yaxis: { title: '利息累积和' }
        });
    </script>
</body>
</html>
